package com.feilong.modules.controller.goods;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feilong.modules.common.constant.UploadConstant;
import com.feilong.modules.common.exception.APIException;
import com.feilong.modules.model.goods.constant.GoodsInfoConstant;
import com.feilong.modules.model.goods.request.QueryGoodsRequest;
import com.feilong.modules.model.goods.vo.GoodsDetailVO;
import com.feilong.modules.model.goods.vo.GoodsInfoVO;
import com.feilong.modules.service.goods.GoodsInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RefreshScope
@RequestMapping("goods/info")
public class GoodsInfoController {

    @Resource
    private GoodsInfoService goodsInfoService;

    /**
     * 从微信云开发服务器获取商品详情
     * @param id
     * @return
     */
    @GetMapping("getGoodsInfo/{id}")
    public GoodsDetailVO getGoodsInfo(@PathVariable("id") Integer id) {
        if (id == null || id < 1) {
            throw new APIException(GoodsInfoConstant.GOODS_INFO_ERROR);
        }
        return goodsInfoService.getGoodsInfo(id);
    }

    /**
     * 从mysql中获取商品列表或者查询商品
     * @param query
     * @return
     */
    @PostMapping("getGoodsList")
    public Page<GoodsInfoVO> getGoodsList(@RequestBody QueryGoodsRequest query){
        return goodsInfoService.getGoodsList(query);
    }

    /**
     * 不分页的形式根据商品名称获取商品列表
     * @param goodsName
     * @return
     */
    @GetMapping("list")
    public List<GoodsInfoVO> getGoodsByName(@RequestParam("goodsName") String goodsName){
        if (StringUtils.isBlank(goodsName)) {
            throw new APIException(GoodsInfoConstant.GOODS_UNEXIST);
        }
        return goodsInfoService.getGoodsByName(goodsName);
    }

    /**
     * 在微信云开发服务器添加商品信息
     */
    @PostMapping("addGoodsInfo")
    public void addGoodsInfo(@RequestBody GoodsDetailVO goodsInfo) {
        if (Objects.isNull(goodsInfo)) {
            throw new APIException(GoodsInfoConstant.GOODS_INFO_ERROR);
        }
        goodsInfoService.addGoodsInfo(goodsInfo);
    }

    /**
     * 更新微信云开发服务器商品信息
     */
    @PostMapping("updateGoodsInfo")
    public void updateGoodsInfo(@RequestBody GoodsDetailVO goodsInfo){
        if (Objects.isNull(goodsInfo)) {
            throw new APIException(GoodsInfoConstant.GOODS_UPDATE_FAIL);
        }
        goodsInfoService.updateGoodsInfo(goodsInfo);
    }

    /**
     * 上传商品图片到微信云开发服务器
     * 上传后返回图片链接
     * @return
     */
    @PostMapping("uploadGoodsImage")
    public String uploadGoodsImage(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName) {
        if (StringUtils.isBlank(fileName) || file.isEmpty()) {
            throw new APIException(UploadConstant.UPLOAD_FAIL);
        }
        return goodsInfoService.uploadGoodsImage(file, fileName);
    }

}
